import { Context } from "koa"
import send from "koa-send"
import { HttpStatus } from "./responseHandler"

export default async (ctx: Context, next) => {
    if (ctx.path.startsWith('/uploads')) {
        try {
            return await send(ctx, ctx.path, { root: global.config.get('uploadsPath').substring(0, global.config.get('uploadsPath').indexOf('/uploads')) })
        } catch (error) {
            ctx.status = HttpStatus.OK;
            ctx.body = {
                status: HttpStatus.NOT_FOUND,
                msg: '资源不存在'
            }
        }
    }
    return await next()
}